/**
*	插件：树形结构
*	开发者：雨中磐石
*	地址：www.rockoa.com
*	日期：2023-07-17
*/

js.tree = {
	alltree:{},
	getobj:function(id){
		return this.alltree[id];
	},
	create:function(cans){
		var me = this;
		var defv = js.applyIf(cans,{
			rand:js.getrand(),
			data:[],
			pid:'pid',//上级id字段
			firstid:'0',
			active:true,
			openchliren:false, //是否打开二级
			mid:0,
			view:'',
			url:'',
			cls:'rock-list-item rock-hover',
			onitemclick:function(){}
		});
		for(var i in defv)this[i]=defv[i];
		var rand  = this.rand;
		
		this.init = function(){
			this.showitem(this.firstid,this.view, 1, this.openchliren);
			js.tree.alltree[rand] = this;
		};
		
		this.showitem = function(pid,view,lev,sopn){
			var s = '',da=this.data,len=da.length,i,d,ico,sto,dope=[],sid,s1;
			for(i=0;i<len;i++){
				d = da[i];
				if(d[this.pid]==pid){
					if(typeof(d.stotal)=='undefined')d.stotal = this.getstotal(d.id);
					sto = d.stotal;
					this.data[i].stotal = sto;
					s+='<div clx="item" style="padding-left:'+(lev*15)+'px" class="'+this.cls+'" onclick="js.tree.clickdiv('+i+','+lev+',\''+rand+'\',this,event)">';
					if(sto>0){
						sid = 'itemicon_'+rand+'_'+d.id+'';
						s+='<i clx="open" id="'+sid+'" onclick="" class="icon-folder-close-alt"></i> ';
						if(sopn)dope.push({xu:i,id:d.id,lev:lev,sid:sid});
					}else{
						if(d.icons){
							s+='<img src="'+d.icons+'" height="20px" align="absmiddle" class="radius5" width="20px"> ';
						}else{
							s+='<i class="icon-file-alt"></i> ';
						}
					}
					s+=''+d.name+'';
					if(sto>0 && this.showstotal)s+='<span class="hui fontsize14">('+sto+')</span>';
					if(d.subname)s+='<span class="hui fontsize14">('+d.subname+')</span>';
					if(d.inputtype){
						s1='';
						if(d.checked)s1=' checked';
						if(d.disabled)s1=' disabled';
						s+='<font><input xu="'+i+'" onclick="js.tree.clickcheck('+i+',this,\''+rand+'\')" name="inputtype'+rand+'" style="width:18px;height:18px;" type="'+d.inputtype+'" value="'+d.id+'" '+s1+'></font>';
					}
					s+='</div>';
				}
			}
			$('#'+view+'').html(s);
			if(sopn)for(i=0;i<dope.length;i++){
				this.showchliren(dope[i].xu,dope[i].lev, get(dope[i].sid));
			}
		}
		
		this.getstotal=function(pid){
			var da=this.data,len=da.length,i,to=0;
			for(i=0;i<len;i++){
				if(da[i][this.pid]==pid)to++;
			}
			return to;
		}
		
		this.showchliren=function(i, lev,o1){
			var d = this.data[i];
			if(d.stotal>0){
				var view = 'menulist'+rand+'_'+d.id+'';
				o1.className = 'icon-folder-open-alt';
				if(!get(view)){
					$(o1).parent().after('<div id="'+view+'"></div>');
					this.showitem(d.id, view, lev+1);
				}else{
					$('#'+view+'').toggle();
					if(get(view).style.display=='none')o1.className = 'icon-folder-close-alt';
				}
			}
		}
		
		this.clickdiv = function(i,lev,o1,e){
			var o   = e.target;
			var lxs = o.nodeName.toLowerCase();
			var clx = $(o).attr('clx');
			if(clx=='open'){
				this.showchliren(i,lev, o);
			}
			if(clx=='item'){
				if(this.active){
					$('#'+this.view+'').find('div').removeClass('rock-active');
					$(o1).addClass('rock-active');
				}
				var d = this.data[i];
				this.onitemclick(d);
			}
		}
		
		this.getchecked = function(){
			var ds = [],i,len,o;
			len = this.data.length;
			for(i=0;i<len;i++){
				o = this.data[i];
				if(!o.disabled && o.checked){
					ds.push(o);
				}
			}
			return ds;
		}
		
		this.getcheckedone = function(){
			var ds = [],i,len,o,xu;
			var obj = $('#'+this.view+'').find('input[name=inputtype'+rand+']');
			len = obj.length;
			for(i=0;i<len;i++){
				o = obj[i];
				if(!o.disabled && o.checked){
					xu = parseFloat($(o).attr('xu'));
					ds.push(this.data[xu]);
				}
			}
			return ds;
		}
		
		this.remove=function(){
			$('#'+this.view+'').html('');
			js.tree.alltree[rand] = false;
		}
		
		this.clickcheck=function(i,o1){
			this.data[i].checked = o1.checked;
		}
		
		this.init();
	},

	clickdiv:function(i,lev,rnd,o1,e){
		var obj = this.getobj(rnd);
		obj.clickdiv(i,lev,o1,e);
	},
	clickcheck:function(i,o1,rnd){
		var obj = this.getobj(rnd);
		obj.clickcheck(i,o1);
	}
}